package com.vader.common.service.impl;

import com.vader.common.entity.DO.GeneratorConfigDO;
import com.vader.common.mapper.GeneratorConfigMapper;
import com.vader.common.service.GeneratorConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;

/**
 * @author Vader
 * @date 2021/10/5
 **/
@Service
public class GeneratorConfigServiceImpl implements GeneratorConfigService {

    @Resource
    private GeneratorConfigMapper generatorConfigMapper;

    @Override
    public GeneratorConfigDO find(String tableName) {
        return generatorConfigMapper.findByTableName(tableName);
    }

    @Override
    public GeneratorConfigDO update(String tableName, GeneratorConfigDO genConfig) {
        String separator = File.separator;
        String[] paths;
        String symbol = "\\";
        if (symbol.equals(separator)) {
            paths = genConfig.getPath().split("\\\\");
        } else {
            paths = genConfig.getPath().split(File.separator);
        }
        StringBuilder api = new StringBuilder();
        for (String path : paths) {
            api.append(path);
            api.append(separator);
            if ("src".equals(path)) {
                api.append("api");
                break;
            }
        }
        genConfig.setApiPath(api.toString());
        return generatorConfigMapper.update(genConfig);
    }
}
